/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.sdkj.dspservice.common.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sdkj.dspservice.common.base.dao.CrudDao;
import com.sdkj.dspservice.common.base.entity.DataEntity;
import com.sdkj.dspservice.common.base.service.CrudService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Service基类
 * @author ThinkGem
 * @version 2014-05-16
 */
@Transactional(readOnly = true)
public abstract class CrudServiceImpl<D extends CrudDao<T>, T extends DataEntity<T>> extends BaseServiceImpl<D, T> implements CrudService<T> {
	
	/**
	 * 持久层对象
	 */
	@Autowired
	protected D dao;

	public List<T> list(T t) {
		return list(new QueryWrapper<>(t));
	}

	@Override
	public List<T> list(Wrapper<T> queryWrapper) {
		((QueryWrapper) queryWrapper).orderByDesc("update_date");
		return dao.selectList(queryWrapper);
	}

	@Override
	public List<T> list() {
		QueryWrapper queryWrapper = new QueryWrapper();
		queryWrapper.orderByDesc("update_date");
		return dao.selectList(queryWrapper);
	}

	@Override
	public <E extends IPage<T>> E page(E page, Wrapper<T> queryWrapper) {
		((QueryWrapper) queryWrapper).orderByDesc("update_date");
		return dao.selectPage(page, queryWrapper);
	}

	@Override
	public <E extends IPage<T>> E page(E page) {
		QueryWrapper<T> queryWrapper = new QueryWrapper();
		queryWrapper.orderByDesc("update_date");
		return dao.selectPage(page, queryWrapper);
	}

	@Override
	public List<T> listLink(Wrapper<T> wrapper) {
		((QueryWrapper)wrapper).eq("a.delete_flag", "0");
		((QueryWrapper)wrapper).orderByDesc("a.update_date");
		return dao.listLink(wrapper);
	}
}
